package com.alipay.app.groupmeal.standard.bean;

import com.alipay.app.groupmeal.standard.util.ByteUtil;
import com.alipay.app.groupmeal.standard.util.HexDump;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SerialPortReceive {

    private String head; //包头、起始符

    private String packetLength; //包长度

    private String requestCode; //请求码, 01查询余额; 02请求支付; 03交易未决

    private String packetSerialNumber; //包序号;00~FF

    private String retainArea; //保留区域

    private String cardSn; //卡序列号

    private String costAmount; //消费金额

    private String foodLength; //菜品长度

    private String foodString; //菜品信息字符串

    private String verifyCode; //校验位

    private String foot; //包尾、结束符

    private static SerialPortReceive instance;

    public static synchronized SerialPortReceive getInstance() {
        if (instance == null) {
            instance = new SerialPortReceive();
        }
        return instance;
    }

    public void receiveData(String receiveText) {
        this.head = receiveText.substring(0, 4);
        this.packetLength = receiveText.substring(4, 8);
        this.requestCode = receiveText.substring(8, 10);
        this.packetSerialNumber = receiveText.substring(10, 12);
        this.retainArea = receiveText.substring(12, 28);
        this.cardSn = receiveText.substring(28, 36);
        this.costAmount = receiveText.substring(36, 44);
        this.foodLength = receiveText.substring(44, 48);
        this.foodString = receiveText.substring(48, receiveText.length() - 4);
        this.verifyCode = receiveText.substring(receiveText.length() - 4, receiveText.length() - 2);
        this.foot = receiveText.substring(receiveText.length() - 2);
    }

    public String getHead() {
        return head;
    }

    public String getPacketLength() {
        return packetLength;
    }

    public String getRequestCode() {
        return requestCode;
    }

    public String getPacketSerialNumber() {
        return packetSerialNumber;
    }

    public String getRetainArea() {
        return retainArea;
    }

    public String getCardSn() {
        return cardSn;
    }

    public String getCostAmount() {
        byte[] temp = ByteUtil.hexStringToByte(costAmount);
        byte[] byteArray = new byte[temp.length];
        for (int i = 0; i < temp.length; i++) {
            byteArray[i] = temp[temp.length - 1 - i];
        }
        String finalString = HexDump.toHexString(byteArray);
        BigDecimal amount = new BigDecimal(Integer.parseInt(finalString, 16)).divide(new BigDecimal(100));
        return amount.toPlainString();
    }

    public String getFoodLength() {
        return foodLength;
    }

    public String getFoodString() throws UnsupportedEncodingException {
        // 编码成GB2312字节数组
        byte[] gb2312Bytes = ByteUtil.hexStringToByte(foodString);
        // 解码回字符串
        String decodedString = new String(gb2312Bytes,"GB2312");
        System.out.println("解析结果:" + decodedString);
        return decodedString;
    }

    public String getVerifyCode() {
        return verifyCode;
    }

    public String getFoot() {
        return foot;
    }

    public List<FoodBean> getFoodBeanList() throws Exception {
        Map<String, Integer> map = new HashMap<>();
        String[] array = getFoodString().split("&");
        for (String string : array) {
            if(!map.containsKey(string)) {
                map.put(string, 0);
            }
            Integer count = map.get(string);
            assert count != null;
            count++;
            map.put(string, count);
        }
        List<FoodBean> resList = new LinkedList<>();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            String[] split = key.split(",");
            FoodBean foodBean = new FoodBean();
            foodBean.setPrice(split[0]);
            foodBean.setFoodName(split[1]);
            foodBean.setNumber(map.get(key));
            resList.add(foodBean);
        }
        return resList;
    }

    public boolean verify(String target) {
        String verifyString = this.requestCode + this.packetSerialNumber + this.retainArea
                + this.cardSn + this.costAmount + this.foodLength + this.foodString;
        int sum = 0;
        for (int i = 0; i < verifyString.length() / 2; i++) {
            String substring = verifyString.substring(i * 2, i * 2 + 2);
            sum += Integer.parseInt(substring, 16);
        }
        String hexString = Integer.toHexString(sum).toUpperCase();
        String verify = hexString.substring(hexString.length() - 2);
        return target.equals(verify);
    }
}
